package com.ss.summer.dao;


import com.ss.summer.core.exceptions.SummerCustomSqlException;
import com.ss.summer.sql.core.CustomSql;
import com.ss.summer.sql.core.SummerQuery;
import com.ss.summer.sql.model.SummerApply;
import com.ss.summer.sql.model.SummerPage;
import com.ss.summer.sql.statics.CustomSqlMap;
import com.ss.summer.sql.statics.SummerSqlConstant;
import com.ss.summer.sql.utils.SqlTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/** 查询Plus
 * @author xia17
 * @date 2019/8/8 16:02
 */
@Repository
public class SummerInstanceDao {

    private JdbcTemplate jdbcTemplate;


    @Autowired
    SummerInstanceDao(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 获取待审核List
     * @param apiPath 凭证
     * @param memberId 审核人
     * @param query 查询条件
     * @param clz 申请的对象
     * @param <T> 申请的对象
     * @return List
     */
    public<T extends SummerApply> List<T> findWaitAudit(String apiPath, String memberId, SummerQuery query, Class<T> clz)  {
        String sqlTemplate = SummerSqlConstant.FIND_WAIT_AUDIT;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId);
        //封装成Obj
        return customSql.toEntityList(maps,clz);
    }


    /**
     * 获取待审核分页
     * @param apiPath 凭证
     * @param memberId 审核人Id
     * @param query 查询条件
     * @param page 页数
     * @param size 大小
     * @param clz 申请表的泛型
     * @param <T> 泛型T
     * @return 分页
     */
    public<T extends SummerApply> SummerPage<T> findWaitAudit(String apiPath, String memberId, SummerQuery query , Class<T> clz , int page, int size)  {
        //sql语句
        String sqlTemplate = SummerSqlConstant.FIND_WAIT_AUDIT_PAGE;
        String countSqlTemplate = SummerSqlConstant.FIND_WAIT_AUDIT_COUNT;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        String countSql = SqlTemplateUtil.count(countSqlTemplate,customSql,query,page,size);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId);
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, apiPath, memberId);
        if (count == null){
            throw new SummerCustomSqlException("查询异常");
        }
        //封装成Obj
        List<T> list = customSql.toEntityList(maps, clz);
        return new SummerPage<>(count, list);
    }


    /**
     * 获取已审核List
     * @param apiPath 凭证
     * @param memberId 审核人
     * @param query 查询条件
     * @param clz 申请的对象
     * @param <T> 申请的对象
     * @return List
     */
    public<T extends SummerApply> List<T> findAudited(String apiPath, String memberId, SummerQuery query, Class<T> clz)  {
        String sqlTemplate = SummerSqlConstant.FIND_AUDITED;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId);
        //封装成Obj
        return customSql.toEntityList(maps,clz);
    }

    /**
     * 获取已审核分页
     * @param apiPath 凭证
     * @param memberId 审核人Id
     * @param query 查询条件
     * @param page 页数
     * @param size 大小
     * @param clz 申请表的泛型
     * @param <T> 泛型T
     * @return 分页
     */
    public<T extends SummerApply> SummerPage<T> findAudited(String apiPath, String memberId, SummerQuery query , Class<T> clz ,int page, int size)  {
        //sql语句
        String sqlTemplate = SummerSqlConstant.FIND_AUDITED_PAGE;
        String countSqlTemplate = SummerSqlConstant.FIND_AUDITED_COUNT;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        String countSql = SqlTemplateUtil.count(countSqlTemplate,customSql,query,page,size);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId);
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, apiPath, memberId);
        if (count == null){
            throw new SummerCustomSqlException("查询异常");
        }
        //封装成Obj
        List<T> list = customSql.toEntityList(maps, clz);
        return new SummerPage<>(count, list);
    }



    /**
     * 获取有权限的
     * @param apiPath 凭证
     * @param memberId 审核人
     * @param query 查询条件
     * @param clz 申请的对象
     * @param <T> 申请的对象
     * @return List
     */
    public<T extends SummerApply> List<T> findHasAuth(String apiPath, String memberId, SummerQuery query, Class<T> clz)  {
        String sqlTemplate = SummerSqlConstant.FIND_HAS_AUTH;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId,apiPath);
        //封装成Obj
        return customSql.toEntityList(maps,clz);
    }

    /**
     * 获取已审核分页
     * @param apiPath 凭证
     * @param memberId 审核人Id
     * @param query 查询条件
     * @param page 页数
     * @param size 大小
     * @param clz 申请表的泛型
     * @param <T> 泛型T
     * @return 分页
     */
    public<T extends SummerApply> SummerPage<T> findHasAuth(String apiPath, String memberId, SummerQuery query , Class<T> clz ,int page, int size)  {
        //sql语句
        String sqlTemplate = SummerSqlConstant.FIND_HAS_AUTH_PAGE;
        String countSqlTemplate = SummerSqlConstant.FIND_HAS_AUTH_COUNT;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        String countSql = SqlTemplateUtil.count(countSqlTemplate,customSql,query,page,size);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,apiPath,memberId,apiPath);
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, apiPath, memberId,apiPath);
        if (count == null){
            throw new SummerCustomSqlException("查询异常");
        }
        //封装成Obj
        List<T> list = customSql.toEntityList(maps, clz);
        return new SummerPage<>(count, list);
    }


    /**
     * 获取他人已审核List
     * @param apiPath 凭证
     * @param memberId 审核人
     * @param query 查询条件
     * @param clz 申请的对象
     * @param <T> 申请的对象
     * @return List
     */
    public<T extends SummerApply> List<T> findAuditedForOther(String apiPath, String memberId, SummerQuery query, Class<T> clz)  {
        String sqlTemplate = SummerSqlConstant.FIND_AUDITED_OTHER;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,memberId , apiPath);
        //封装成Obj
        return customSql.toEntityList(maps,clz);
    }

    /**
     * 获取他人已审核分页
     * @param apiPath 凭证
     * @param memberId 审核人Id
     * @param query 查询条件
     * @param page 页数
     * @param size 大小
     * @param clz 申请表的泛型
     * @param <T> 泛型T
     * @return 分页
     */
    public<T extends SummerApply> SummerPage<T> findAuditedForOther(String apiPath, String memberId, SummerQuery query , Class<T> clz ,int page, int size)  {
        //sql语句
        String sqlTemplate = SummerSqlConstant.FIND_AUDITED_OTHER_PAGE;
        String countSqlTemplate = SummerSqlConstant.FIND_AUDITED_OTHER_COUNT;
        //获取申请表的一些参数信息
        CustomSql customSql = CustomSqlMap.get(clz);
        //替换sql语句中的动态参数
        String sql = SqlTemplateUtil.base(sqlTemplate,customSql,query);
        String countSql = SqlTemplateUtil.count(countSqlTemplate,customSql,query,page,size);
        //查询
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,memberId , apiPath);
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, memberId, apiPath);
        if (count == null){
            throw new SummerCustomSqlException("查询异常");
        }
        //封装成Obj
        List<T> list = customSql.toEntityList(maps, clz);
        return new SummerPage<>(count, list);
    }



}
